package com.itheima.controller;


import com.itheima.common.Code;
import com.itheima.common.MessageConstant;
import com.itheima.common.Result;
import com.itheima.entity.AddressBook;
import com.itheima.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/*
 *地址模块
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressController {
    @Autowired
    private AddressService addressService;
        /*
        新增用户
         */
    @PostMapping
    public Result saveAddress(@RequestBody AddressBook addressBook){
       int row =  addressService.saveAddress(addressBook);

       if(row <1){
           //如果row执行小于1就是失败返回提示
           return new Result(Code.ERROR, MessageConstant.ADDRESS_SAVE_ERROR);
       }
       //成功便是返回成功提示
        return new Result(Code.OK,MessageConstant.ADDRESS_SAVE_SUCCESS);
    }
    /**
     * 根据当前登录用户,查询用户地址列表
     *2
     */
    @GetMapping("/list")
    public Result list(){
        List<AddressBook> addressBookList = addressService.list();
        if (addressBookList == null){
           addressBookList = new ArrayList<>();
        }
        return new Result(Code.OK,MessageConstant.ADDRESS_SAVE_SUCCESS,addressBookList);
    }
    /**
     * 修改用户默认地址
     * @param :add
     * @return
     */
    @PutMapping("/default")
    public Result dafaultAddr(@RequestBody AddressBook add){
        System.out.println("请求参数"+add.getId());
        int row = addressService.defaultAddr(add.getId());
        if(row <1){
            return new Result(Code.ERROR,MessageConstant.UPDATE_DEFAULT_ADDRESS_ERROR);

        }
        //封装结果
        return new Result(Code.OK,MessageConstant.UPDATE_DEFAULT_ADDRESS_SUCCESS);
    }
    /**
     * 更新地址
     * @return
     */
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook){
        int row = addressService.update(addressBook);
        if (row<1){
            return new Result(Code.ERROR,MessageConstant.CAT_UPDATE_ERROR);
        }
        return new Result(Code.OK,MessageConstant.CAT_UPDATE_SUCCESS);
    }
    /**
     * 查询用户默认地址
     */

    @GetMapping("/default")
    public Result getDefaultAddr(){
        AddressBook defaultAddr = addressService.getDefaultAddr();
        if (defaultAddr == null){
            return new Result(Code.ERROR,MessageConstant.ADDRESS_SAVE_ERROR);
        }
        return new Result(Code.OK,MessageConstant.ADDRESS_SAVE_SUCCESS,defaultAddr);

    }

    /**
     * 根据id获取地址
     * @param :ids
     * @return
     */
    @GetMapping("/{id}")
    public Result getAddress(@PathVariable("id")Long id){
        AddressBook adderss = addressService.getAdderss(id);
        //返回结果
        if (adderss == null){
            return new Result(Code.ERROR,MessageConstant.ADDRESS_GET_ERROR);
        }
        return  new Result(Code.OK,MessageConstant.ADDRESS_GET_SUCCESS,adderss);
    }

    /**
     * 删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result deleteAddress(@RequestParam("ids") Long ids){
        int row = addressService.dedeleteAddress(ids);
        if (row == 0){
            return new Result(Code.ERROR,MessageConstant.ADDRESS_DEL_ERROR);
        }

        return  new Result(Code.OK,MessageConstant.ADDRESS_DEL_SUCCESS);
    }
}
/**
 * @BelongsProject: reggie_project
 * @BelongsPackage: com.itheima.controller
 * @Author: luanyinghao
 * @CreateTime: 2022-06-05  16:45
 * @Description: TODO
 * @Version: 1.0
 **/